import robot from 'robotjs'
import clipboard from 'clipboardy'
import inquirer from 'inquirer'

let posX,
  posY,
  duration = 3000 / 1000

console.log('请在3秒内将鼠标移动到微信图标以获取坐标')
await new Promise(resolve => {
  const timer = setInterval(() => {
    console.log('剩余', duration + '秒')
    duration--
    if (duration <= 0) {
      let { x, y } = robot.getMousePos()
      posX = x
      posY = y
      resolve()
      clearInterval(timer)
    }
  }, 1000)
})
console.log('获取坐标成功, 当前坐标:', posX + ',' + posY)

const question = [
  {
    type: 'input',
    name: 'content',
    message: '请输入要发送的内容',
    default: 'hello world'
  },
  {
    type: 'number',
    name: 'times',
    message: '请输入重复次数',
    default: 1
  },
  {
    type: 'confirm',
    name: 'isSuffix',
    message: '是否添加后缀',
    default: false
  }
]

const { content, times, isSuffix } = await inquirer.prompt(question)

robot.moveMouse(posX, posY)
robot.mouseClick()

for (let i = 1; i < times + 1; i++) {
  clipboard.writeSync(content + ' ' + (isSuffix ? i : ''))
  robot.keyTap('v', 'control')
  robot.keyTap('enter')
}
robot.keyTap('escape')
console.log('程序执行完毕！')
